package com.goldcn.ftp.controller;

import com.goldcn.ftp.service.FtpService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * @Author
 * mail:
 * Date: 2017/10/16
 * Time: 10:46
 * To change this template use File | Settings | File Templates.
 */
@RestController
public class FtpController {


    @Autowired
    FtpService ftpService;

    private static final String temp_path = System.getProperty("user.dir") + File.separator + "image" + File.separator;

    /**
     * oss 文件上传
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/ftp/file")
    public Object uploadfile(HttpServletResponse response, HttpServletRequest request,
                             @RequestParam(value = "file", required = false) MultipartFile file,
                             @RequestParam(value = "type", required = false) String type){
        try {
            return JSONObject.fromObject(ftpService.uploadFiles(file));
        }catch (Exception e){
            e.printStackTrace();
        }
        return "error";
    }


    /**
     * 文件下载
     * @param fileName
     * @param response
     * @param measureType
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/ftp/download/{fileName:.+}")
    public void downLoad(@PathVariable String fileName, HttpServletRequest request, HttpServletResponse response,
                           @RequestParam(value = "type", required = false) String measureType) {
        try {
            ftpService.downLoad(fileName,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 文件删除
     * @param fileName
     * @return
     * @throws IOException
     */
    @RequestMapping(method = RequestMethod.GET, value = "/ftp/deleteFtpFile/{fileName:.+}")
    public Object deleteFtpFile(@PathVariable String fileName) throws IOException {
        return JSONObject.fromObject(ftpService.deleteFtpFile(fileName));
    }


    /**
     * oss 文件上传
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/ftp/test")
    public Object test(){
        try {
            return JSONObject.fromObject(ftpService.uploadFiles1());
        }catch (Exception e){
            e.printStackTrace();
        }
        return "error";
    }
}
